package com.compty.paint.web.member;


import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONObject;

import com.compty.core.framework.util.DateUtil;
import com.compty.core.struts2.BaseAction;
import com.compty.paint.domain.AddressInfo;
import com.compty.paint.domain.OrderItem;
import com.compty.paint.manager.AddressInfoManager;
import com.compty.paint.manager.OrderItemManager;
import com.compty.paint.util.Constants;
import com.compty.security.domain.User;

/**
 * 商户会员管理订单Action
 */
public class OrderAction extends BaseAction<OrderItem>{

	private static final long serialVersionUID = 1484515018233178666L;
	
	private OrderItemManager orderItemManager;
	private AddressInfoManager addressInfoManager;
	
	private Date startDate;
	private Date endDate;
	private String sequenceNo;
	
	/**
	 *@Todo:订单列表
	 *@CreateTime:2011-6-17 上午11:51:50
	 */
	public String list(){
		
		String hql = " from OrderItem where 1=1 ";
        Object[] param = new Object[0];
        
        User userInfo = (User) session.getAttribute(Constants.Userinfo);
        if(userInfo != null  && "1".equals(userInfo.getAdministratorFlag())){
        	String userName = userInfo.getUsername();
        	if(Constants.SuperAdminstrator.equals(userName)){// 超级管理员
                if( startDate != null && !"".equals(startDate)){
                  	 hql += " and order.orderTime >= ? ";
                  	 param = org.apache.commons.lang.ArrayUtils.add(param, startDate);
                  }
                  
                  if( endDate != null && !"".equals(endDate)){
                  	 hql += " and order.orderTime < ? ";
                  	 param = org.apache.commons.lang.ArrayUtils.add(param, endDate);
                  }
                  
                  hql +=" order by id desc";
                  this.page = orderItemManager.pagedQuery(hql, curPage, pageSize,param);
        	}
        }else{
          if( userInfo != null ){
           	 hql += "  and belongTo = ? ";
           	 param = org.apache.commons.lang.ArrayUtils.add(param, userInfo.getId());
           }
          
           if( startDate != null && !"".equals(startDate)){
           	 hql += " and order.orderTime >= ? ";
           	 param = org.apache.commons.lang.ArrayUtils.add(param, startDate);
           }
           
           if( endDate != null && !"".equals(endDate)){
           	 hql += " and order.orderTime < ? ";
           	 param = org.apache.commons.lang.ArrayUtils.add(param, endDate);
           }
           
           hql +=" order by id desc";
           this.page = orderItemManager.pagedQuery(hql, curPage, pageSize,param);
        }
        
        return SUCCESS;
	}
	
	/**
	 * 由订单号查找订单
	 */
	public String findBySequenceNo(){
		
		String hql = " from OrderItem where 1=1 ";
        Object[] param = new Object[0];
        
        User userInfo = (User) session.getAttribute(Constants.Userinfo);
        if(userInfo != null  && "1".equals(userInfo.getAdministratorFlag())){
        	String userName = userInfo.getUsername();
        	if(Constants.SuperAdminstrator.equals(userName)){
                if( sequenceNo != null && !"".equals(sequenceNo)){
                	 hql += " and order.sequenceNo = ? ";
                	 param = org.apache.commons.lang.ArrayUtils.add(param, sequenceNo);
                }

                hql +=" order by id desc";
                this.page = orderItemManager.pagedQuery(hql, curPage, pageSize,param);
        	}
        }else{
        	  if( userInfo !=null ){
             	 hql += "  and belongTo = ? ";
             	 param = org.apache.commons.lang.ArrayUtils.add(param, userInfo.getId());
             }
            
             if( sequenceNo != null && !"".equals(sequenceNo)){
             	 hql += " and order.sequenceNo = ? ";
             	 param = org.apache.commons.lang.ArrayUtils.add(param, sequenceNo);
             }

             hql +=" order by id desc";
             this.page = orderItemManager.pagedQuery(hql, curPage, pageSize,param);
        }
		
		 return SUCCESS;
	}
	
	/**
	 * 显示订单详情
	 *@CreateTime:2011-6-20 下午01:55:19
	 */
	@SuppressWarnings("unchecked")
	public String showDetail(){
		
		String orderItmeId = request.getParameter("orderItmeId");
		
		try {
			if( orderItmeId != null){
				
				Long oiId = Long.valueOf(orderItmeId);
				OrderItem orderItem = orderItemManager.get(oiId);
				
				Map map = new HashMap();
				map.put("name", orderItem.getName());
				map.put("sequenceNo", orderItem.getOrder().getSequenceNo());
				map.put("price", orderItem.getPrice());
				map.put("num", orderItem.getNum());
				map.put("orderTime", DateUtil.toString(orderItem.getOrder().getOrderTime(),"yyyy-MM-dd hh:mm"));
				map.put("payment_type", orderItem.getOrder().getPayment_type());
				map.put("fpFlag", orderItem.getOrder().getFpFlag());
				map.put("fpType", orderItem.getOrder().getFpType());
				map.put("fpType2", orderItem.getOrder().getFpType2());
				map.put("fpHead", orderItem.getOrder().getFpHead());
				map.put("orderStatus", orderItem.getOrder().getOrderStatus());
				String ship_name = "";
				String ship_mobile = "";
				String ship_addr = "";
				Integer addressInfo_id = orderItem.getOrder().getAddressInfo_id();
				if( addressInfo_id != null ){
					AddressInfo addressInfo = addressInfoManager.get(addressInfo_id);
					if( addressInfo != null){
						ship_name =  addressInfo.getConsignee();
						ship_mobile = addressInfo.getMobile();
						ship_addr = addressInfo.getProvinceInfo().getName()+addressInfo.getCityInfo().getName()+addressInfo.getAddress();
					}
				}
				map.put("ship_name", ship_name);
				map.put("ship_mobile", ship_mobile);
				map.put("ship_addr", ship_addr);
				String  ajaxData = JSONObject.fromObject(map).toString();
				request.setAttribute("ajaxData", ajaxData);
			}
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
		
		 return SUCCESS;
	}


	public Date getStartDate() {
		return startDate;
	}
	
	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}
	
	public Date getEndDate() {
		return endDate;
	}
	
	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}
	
	public String getSequenceNo() {
		return sequenceNo;
	}

	public void setSequenceNo(String sequenceNo) {
		this.sequenceNo = sequenceNo;
	}

	public void setOrderItemManager(OrderItemManager orderItemManager) {
		this.orderItemManager = orderItemManager;
	}

	public void setAddressInfoManager(AddressInfoManager addressInfoManager) {
		this.addressInfoManager = addressInfoManager;
	}
}
